using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Data;
using System.Web.UI.WebControls;

namespace GMAO.CORE
{
    public class MenuClass
    {
        public static XmlNodeList loadMenu(string NomTable, string ID, string ParentID, string OrderBy, params string[] Colones)
        {
            string sRequete;
            XmlDocument XMLDoc = new XmlDocument();
            DataSet ds = new DataSet();
            StringBuilder sTransformXML = new StringBuilder();
            sRequete = "SELECT " + ID + ", " + ParentID + ", ";
            for (int i = 0; i < Colones.Length; i++)
            {
                sRequete += Colones[i] + ", ";
            }
            sRequete = sRequete.Substring(0, sRequete.Length - 2);
            sRequete += " FROM " + NomTable;

            ds = DataProviderFactoy.GetInstance().ExecuteGetDataSet(sRequete);
            ds.DataSetName = "Tables";
            ds.Tables[0].TableName = "Table";

            DataRelation relation = new DataRelation("ParentChild",
             ds.Tables["Table"].Columns[ID],
             ds.Tables["Table"].Columns[ParentID], true);
            relation.Nested = true;
            ds.Relations.Add(relation);
            XmlDataSource XmlDataConvert = new XmlDataSource();
            sTransformXML.AppendFormat(@"
                <?xml version='1.0' encoding='utf-8' ?>
                <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
                      <xsl:output method='xml' indent='yes' encoding='utf-8'/>
                      <xsl:template match='/Tables'>
                            <MenuItems>
                                  <xsl:call-template name='MenuListing' />
                            </MenuItems>
                      </xsl:template>
                      <xsl:template name='MenuListing'>
                            <xsl:apply-templates select='Table'>
                                  <xsl:sort select='{0}'/>
                            </xsl:apply-templates>
                      </xsl:template>
                      <xsl:template match='Table'>
                            <MenuItem>
                                <xsl:attribute name='ID'>
                                    <xsl:value-of select='ID'/>
                                </xsl:attribute>
            ", OrderBy);
            for (int i = 0; i < Colones.Length; i++)
            {
                sTransformXML.AppendFormat(@"
                              <xsl:attribute name='{0}'>
                                    <xsl:value-of select='{0}'/>
                              </xsl:attribute>
                ",Colones[i]);
            }
            sTransformXML.Append(@"
                                  <xsl:if test='count(Table) >0'>
                                        <xsl:call-template name='MenuListing' />
                                  </xsl:if>
                            </MenuItem>
                      </xsl:template>
                </xsl:stylesheet>
            ");
            XmlDataConvert.Transform = sTransformXML.ToString();
            XmlDataConvert.XPath = "MenuItems/MenuItem";
            XmlDataConvert.EnableCaching = false;
            XmlDataConvert.Data = ds.GetXml();
            XMLDoc = XmlDataConvert.GetXmlDocument();
            XmlNodeList nodeList = XMLDoc.DocumentElement.ChildNodes;
            return nodeList;
        }
    }
}
